package cn.han.jdbc;

import java.sql.*;
import java.util.Date;
import java.util.Properties;

public class Demo {
    public static class ConnectionManager {
        private static final String url = "jdbc:mysql://localhost:3306/cgb2018";
        private static final String username = "root";
        private static final String userpass = "root";

        @SuppressWarnings("deprecation")
        public static void main(String[] args) throws Exception {
            Connection conn = getConnection();
            String s1 = "abc";
            String s2 = "abcd";

            String sql = "insert into users values(null,'abcdef2','abcdef')";

            // 指定返回生成的主键
//            PreparedStatement pstmt = conn.prepareStatement(sql,
//                    Statement.RETURN_GENERATED_KEYS);
            Statement s = conn.createStatement();
            // 如果使用静态的SQL，则不需要动态插入参数
//            pstmt.setString(1, s1);
//            pstmt.setString(2, s2);
            s.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
            // 检索由于执行此 Statement 对象而创建的所有自动生成的键
            ResultSet rs = s.getGeneratedKeys();
            if (rs.next()) {
                Long id = rs.getLong(1);
                System.out.println("数据主键：" + id);
            }
        }

        static public Connection getConnection() throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/cgb2018";
            Connection connection = DriverManager.getConnection(url, "root", "root");
            return connection;
        }

    }
}

